package cn.lyvust.first.manage.controller;


import cn.lyvust.first.common.annotation.SysLogs;
import cn.lyvust.first.common.bean.ResponseCode;
import cn.lyvust.first.common.bean.ResponseResult;
import cn.lyvust.first.common.exception.RequestException;
import cn.lyvust.first.common.util.BeansUtil;
import cn.lyvust.first.manage.dto.param.ChaiTextPram;
import cn.lyvust.first.manage.dto.param.evaluation.ItemAttrParam;
import cn.lyvust.first.manage.entity.ChaiTest;
import cn.lyvust.first.manage.service.ChaiTestService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ggzheng
 * @since 2021-02-09
 */
@Api(tags = {"ChaiTest"})
@RestController
@RequestMapping("/chaiTest")
public class ChaiTestController {
    @Autowired
    private ChaiTestService chaiTestService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询chai_test")
    public ResponseResult list(@RequestBody ChaiTextPram param){
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getId())) {
            wrapper.eq("id", param.getId());
        }
        return ResponseResult.e(ResponseCode.OK, chaiTestService.list(wrapper));
    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "添加chaitext")
    public ResponseResult add(@RequestBody ChaiTextPram param){
        if (StringUtils.isEmpty(param.getUsername())) {
            throw RequestException.fail("添加失败,姓名不能为空!");
        }
        if (StringUtils.isEmpty(param.getPhone())) {
            throw RequestException.fail("添加失败,手机号不能为空!");
        }
        ChaiTest entity = new ChaiTest();
        BeanUtils.copyProperties(param,entity);
        return ResponseResult.e(ResponseCode.OK,chaiTestService.save(entity));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改chaitext")
    @SysLogs("修改chaitext")
    public ResponseResult update(@RequestBody ChaiTextPram param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,所属项目主键ID不能为空!");
        }
        if (StringUtils.isEmpty(param.getUsername())) {
            throw RequestException.fail("修改失败,修改操作人姓名不能为空!");
        }

        ChaiTest entity= chaiTestService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, chaiTestService.updateById(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除chaitext")
    @SysLogs("删除Frisat")
    public ResponseResult delete(@RequestBody ChaiTextPram param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,所属项目主键ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, chaiTestService.removeById(param.getId()));
    }
}
